---
title: "Choropleth maps with {ggplot2}"
author: "Rubén F. Bustillo"
output:
flexdashboard::flex_dashboard:
source_code: embed
orientation: columns
theme: simplex
vertical_layout: fill
---
```{r setup, include=FALSE}
library(flexdashboard)
library(tidyverse)
library(gapminder)
library(mapdata)
library(gganimate)
library(stringr)
library(viridis)
gapminder_codes <- gapminder::country_codes
gapminder <-gapminder::gapminder_unfiltered
gapminder <- gapminder %>%
inner_join(gapminder_codes, by= "country") %>%
mutate(code = iso_alpha)
world <- map_data("world") %>%
filter(region != "Antarctica")
```
[www.rquer.netlify.com](https://rquer.netlify.com/)
Row {.tabset .tabset-fade}
-------------------------------------------------------------
### Base Map
```{r, fig.height=6, fig.width= 12}
# our first try would be this:
# gapminder %>%
# filter(year == 2007) %>%
# inner_join(maps::iso3166 %>%
# select(a3, mapname), by= c(code = "a3")) %>%
# inner_join(world, by= c(mapname = "region")) %>%
# ggplot(aes(long, lat, group= group, fill= lifeExp)) +
# geom_polygon() +
# labs("Malaria deaths in 2000 ")
# But China, Norway and Findland don not appear in the map. The reason is this:
# maps::iso3166 %>% filter( a3 == "NOR" | a3 == "CHN" | a3 == "FIN")
# We modify our dataset removing the brackets using regular expresions:
gapminder_data <- gapminder %>%
inner_join(maps::iso3166 %>%
select(a3, mapname), by= c(code = "a3")) %>%
mutate(mapname = str_remove(mapname, "\\(.*"))
# Our first map:
map_data("world") %>%
tbl_df() %>%
inner_join(gapminder_data, by=c(region= "mapname")) %>%
filter(year == 2007) %>%
ggplot(aes(long, lat, group= group, fill= lifeExp)) +
geom_polygon(color = "white", size= 0.2) +
labs(title="Life Expectancy",
subtitle = "Year: 2007",
caption = "Source: gapminder.org",
fill = "Years") +
theme(plot.title=element_text(size = 18,
hjust = 0.5),
plot.subtitle = element_text(size = 12, hjust = 0.5),
plot.caption = element_text(size = 8, hjust = 1)) +
coord_fixed (ratio = 1.3)
```
### Color gradient
```{r, fig.height=6, fig.width= 12}
map_data("world") %>%
tbl_df() %>%
inner_join(gapminder_data, by=c(region= "mapname")) %>%
filter(year == 2007) %>%
ggplot(aes(long, lat, group= group, fill= lifeExp)) +
geom_polygon(color = "white", size = 0.05) +
scale_fill_gradient2(low = "firebrick1",
high= "cornflowerblue",
midpoint = 60) +
labs(title="Life Expectancy",
subtitle = "Year: 2007",
caption = "Source: gapminder.org",
fill = "Years") +
theme(
axis.line = element_blank(),
axis.text = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank(),
plot.background = element_rect(fill = "snow", color = NA),
panel.background = element_rect(fill = "snow", color = NA),
plot.title = element_text(size = 16, hjust = 0.5),
plot.subtitle = element_text(size = 12, hjust = 0.5),
plot.caption = element_text(size = 8, hjust = 1),
legend.title = element_text(color = "grey40", size = 8),
legend.text = element_text(color = "grey40", size = 7, hjust = 0),
legend.position = c(0.1, 0.25),
legend.background = element_blank(),
plot.margin = unit(c(0.5,2,0.5,1), "cm")) +
coord_fixed (ratio = 1.3)
```
### Color gradient with {Viridis}
```{r, fig.height=6, fig.width= 12}
map_data("world") %>%
tbl_df() %>%
inner_join(gapminder_data, by=c(region= "mapname")) %>%
filter(year == 2007) %>%
ggplot(aes(long, lat, group= group, fill= lifeExp)) +
geom_polygon(color = "white", size = 0.05, alpha = 0.6) +
scale_fill_viridis(option = "C") +
labs(title="Life Expectancy",
subtitle = "Year: 2007",
caption = "Source: gapminder.org",
fill = "Years") +
theme(
axis.line = element_blank(),
axis.text = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank(),
plot.background = element_rect(fill = "snow", color = NA),
panel.background = element_rect(fill = "snow", color = NA),
plot.title = element_text(size = 16, hjust = 0.5),
plot.subtitle = element_text(size = 12, hjust = 0.5),
plot.caption = element_text(size = 8, hjust = 1),
legend.title = element_text(color = "grey40", size = 8),
legend.text = element_text(color = "grey40", size = 7, hjust = 0),
legend.position = c(0.1, 0.25),
legend.background = element_blank(),
plot.margin = unit(c(0.5,2,0.5,1), "cm")) +
coord_fixed (ratio = 1.3)
```
### Adding breaks with `scale_fill_gradientn()`
```{r, fig.height=6, fig.width= 12}
map_data("world") %>%
tbl_df() %>%
inner_join(gapminder_data, by=c(region= "mapname")) %>%
filter(year == 2007) %>%
ggplot(aes(long, lat, group= group, fill= lifeExp)) +
geom_polygon(color = "white", size = 0.1) +
scale_fill_gradientn(colours = terrain.colors(11),
trans = "reverse",
breaks = c(40,45,50,55,60,65,70,75,80,85)) +
labs(title="Life Expectancy",
subtitle = "Year: 2007",
caption = "Source: gapminder.org",
fill = "Years") +
theme_minimal() +
coord_fixed (ratio = 1.3) +
theme(plot.title = element_text(size = 16, hjust = 0.5),
plot.subtitle = element_text(size = 12, hjust = 0.5),
plot.caption = element_text(size = 8, hjust = 1))
```
### Logarithmic scale
```{r, fig.height=6, fig.width= 12}
options(scipen=10000)
map_data("world") %>%
tbl_df() %>%
inner_join(gapminder_data, by=c(region= "mapname")) %>%
filter(year == 2007) %>%
ggplot(aes(long, lat, group= group, fill= pop)) +
geom_polygon(color = "darkgrey", size = 0.7, alpha = 0.5) +
scale_fill_gradientn(colours = rainbow(11),
trans = "log10",
breaks = c(10000, 100000,1000000, 10000000, 100000000, 1000000000),
labels = scales::comma) +
theme_void() +
labs(title="Population by country in 2007 ",
fill ="Population (log scale)") +
theme(plot.title=element_text(size = 16,
hjust = 0.5))+
coord_fixed (ratio = 1.3)
```
### Year intervals
```{r, fig.height=6, fig.width= 12}
map_data("world") %>%
tbl_df() %>%
inner_join(gapminder_data, by=c(region= "mapname")) %>%
filter(year == 2007) %>%
mutate(groups = case_when(
lifeExp > 0 & lifeExp <= 40 ~ "Less than 40",
lifeExp > 40 & lifeExp <= 50 ~ "between 40 & 50",
lifeExp > 50 & lifeExp <= 60 ~ "between 50 & 60",
lifeExp > 60 & lifeExp <= 70 ~ "between 60 & 70",
lifeExp > 70 & lifeExp <= 80 ~ "between 70 & 80",
TRUE ~ "More than 80"
)) %>%
ggplot(aes(long, lat, group= group, fill= groups)) +
geom_polygon(color = "black", size = 0.2, alpha = 0.8) +
theme_void() +
labs(title="Life expectancy by country in 2007",
fill = "") +
coord_fixed (ratio = 1.3) +
theme(plot.title=element_text(size = 16,
hjust = 0.5),
panel.background = element_rect(colour = "grey", size = 1),
legend.position = "bottom") +
scale_fill_brewer(palette = "Spectral")
```
### Facets: `facet_wrap(~year)`
```{r, fig.height=6, fig.width= 12}
map_data("world") %>%
tbl_df() %>%
inner_join(gapminder_data, by=c(region= "mapname")) %>%
filter(year %in% c(1992, 1997, 2002, 2007)) %>%
ggplot(aes(long, lat, group= group, fill= lifeExp)) +
geom_polygon(color = "white", size = 0.05, alpha = 0.8) +
scale_fill_viridis(
option= "magma",
direction = -1,
name = "Life Expect.",
guide =guide_colorbar(
direction = "horizontal",
barheight = unit(2, units = "mm"),
barwidth = unit(50, units = "mm"),
draw.ulim = F,
title.position = "top",
title.hjust = 0.5,
label.hjust = 0.5
)) +
theme_void() +
facet_wrap(~year) +
labs(title="Life Expectancy by selected years ") +
coord_fixed (ratio = 1.3) +
theme(plot.title=element_text(size = 16,
hjust = 0.5),
legend.position = "bottom")
```
### Subregion with `xlim()` & `ylim()`
```{r}
map_data("world") %>%
tbl_df() %>%
inner_join(gapminder_data, by=c(region= "mapname")) %>%
filter(year == 2007) %>%
ggplot(aes(long, lat, group= group, fill= lifeExp)) +
geom_polygon(color = "white", size = 0.1) +
scale_fill_gradientn(colours = terrain.colors(11),
trans = "reverse",
breaks = c(40,45,50,55,60,65,70,75,80,85)) +
labs(title="Life Expectancy",
subtitle = "Year: 2007",
caption = "Source: gapminder.org",
fill = "Years") +
theme_minimal() +
coord_fixed (ratio = 1.3) +
theme(plot.title = element_text(size = 12, hjust = 0.5),
plot.subtitle = element_text(size = 8, hjust = 0.5),
plot.caption = element_text(size = 5, hjust = 1),
legend.title = element_text(color = "brown", size = 8),
legend.text = element_text(color = "limegreen", size = 6),
axis.text.x = element_text(face = "italic", colour = "grey", size = rel(0.5)),
axis.text.y = element_text(face = "italic", colour = "grey", size = rel(0.5)),
axis.title.x = element_text(colour = "darkgrey", size = rel(0.7)),
axis.title.y = element_text(colour = "darkgrey", size = rel(0.7))) +
xlim(c(-30, 65)) +
ylim(c(-45, 75))
```
### Subregion with `filter()`
```{r}
map_data("world") %>%
tbl_df() %>%
inner_join(gapminder_data, by=c(region= "mapname")) %>%
filter(year == 2007) %>%
filter(continent == "Asia") %>%
ggplot(aes(long, lat, group= group, fill= lifeExp)) +
geom_polygon(color = "lightgrey", size = 0.05) +
scale_fill_gradient2(low = "firebrick1",
high= "cornflowerblue",
midpoint = 65) +
labs(title="Life Expectancy in Asia",
subtitle = "Year: 2007",
caption = "Source: gapminder.org",
fill = "Years") +
theme(
axis.line = element_blank(),
axis.text = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank(),
plot.background = element_rect(fill = "snow", color = NA),
panel.background = element_rect(fill= "snow", color = NA),
plot.title = element_text(size = 16, hjust = 0.5),
plot.subtitle = element_text(size = 12, hjust = 0.5),
plot.caption = element_text(size = 8, hjust = 1),
legend.title = element_text(color = "grey40", size = 8),
legend.text = element_text(color = "grey40", size = 7, hjust = 0),
legend.position = c(0.05, 0.1),
plot.margin = unit(c(0.5,2,0.5,1), "cm")) +
coord_fixed (ratio = 1.3)
```
### Highlighting a region
```{r, fig.height=6, fig.width= 12}
gapminder_data%>%
filter(year == 2007) %>%
filter(continent == "Asia") %>%
right_join(world, by= c(mapname = "region")) %>%
ggplot(aes(long, lat, group= group, fill= lifeExp)) +
geom_polygon(color = "white", size = 0.01) +
theme_void() +
scale_fill_viridis(option = "B") +
labs(title="Life Expectancy in Asia",
subtitle = "Year: 2007",
caption = "Source: gapminder.org",
fill = "Years") +
theme(
axis.line = element_blank(),
axis.text = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank(),
plot.background = element_rect(fill = "snow", color = NA),
panel.background = element_rect(fill= "snow", color = NA),
plot.title = element_text(size = 16, hjust = 0.5),
plot.subtitle = element_text(size = 12, hjust = 0.5),
plot.caption = element_text(size = 8, hjust = 1),
legend.title = element_text(color = "grey40", size = 8),
legend.text = element_text(color = "grey40", size = 7, hjust = 0),
legend.position = c(0.05, 0.1),
plot.margin = unit(c(0.5,2,0.5,1), "cm")) +
coord_fixed (ratio = 1.3)
```